Adding Tags to widgets
Add tags to your existing widgets.
By adding tags, you can track views in your app. This lets you dynamically manage the display and content of your widgets through the Nudge Dashboard—without having to release new app versions.
warning
The tag for each widget must be unique.
XML
If you’re using standard Android views in XML, you can assign a tag directly in the layout file:
<TextView
android:id="@+id/sampleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, Nudge!"
android:tag="nudge_tag" />
Kotlin or Java
- Kotlin
- Java
val textView = findViewById<TextView>(R.id.sampleTextView)
textView.tag = "your_tag" // Set "your_tag"
TextView textView = findViewById(R.id.sampleTextView);
textView.setTag("your_tag"); // Set "your_tag"
Jetpack Compose
If your app uses Jetpack Compose, you’ll need to track views a bit differently. Follow these steps:
Create a Custom Modifier
Add the below snippet to the MainActivity.kt
file in your Project
fun Modifier.NTracker(label: String) = composed {
onGloballyPositioned { layoutCoordinates ->
val left = layoutCoordinates.positionInWindow() .x
val top = layoutCoordinates.positionInWindow() .y
val width = layoutCoordinates.size.width.toFloat()
val height = layoutCoordinates.size.height.toFloat()
Nudge.getInstance().addComposeView(label,left,top,height,width)
}.also {
DisposableEffect(Unit) {
onDispose {
Nudge.getInstance().removeComposeView(label)
}
}
}
}
Apply the Modifier to Your Composable
Use your new NTracker
modifier in any composable element you want to track:
Text(
text = "Welcome to Home!",
modifier = Modifier.NTracker("home_screen_label")
)
note
Make sure the NTracker
modifier always present at the end, after adding all other modifier